<?php
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
namespace app\xs\service;

use think\facade\Db;
use think\Validate;

class XsgzService
{
    /**
     * 显示资源列表
     */
    public function getdj($w = [], $limit = [])
    {
        $w1 = ['delete_time' => '0'];

        //查找单据表是否有这个日期了
        $dj = Db::table('xs_gzdj')->where($w)->where($w1)->find();
        if (empty($dj)) return [];

        //查找 客户工作 表是否有这个单据数据
        $comp1 = Db::table('xs_khgz')->where($w1)->where(['djbm' => $dj['djbm'], 'type' => 1])->select();
        if ($comp1) $dj['comp1'] = $comp1;
        $comp4 = Db::table('xs_khgz')->where($w1)->where(['djbm' => $dj['djbm'], 'type' => 2])->select();
        if ($comp4) $dj['comp4'] = $comp4;

        //查找 其它工作 表是否已有这个单据数据
        $comp2 = Db::table('xs_qtgz')->where($w1)->where(['djbm' => $dj['djbm'], 'type' => 1])->select();
        if ($comp2) $dj['comp2'] = $comp2;
        $comp5 = Db::table('xs_qtgz')->where($w1)->where(['djbm' => $dj['djbm'], 'type' => 2])->select();
        if ($comp5) $dj['comp5'] = $comp5;

        //查找 工作问题 表是否有这个单据数据
        $comp3 = Db::table('xs_gzwt')->where($w1)->where(['djbm' => $dj['djbm']])->select();
        if ($comp3) $dj['comp3'] = $comp3;

        return $dj;
    }

    /**
     * 保存新建的资源
     */
    public function save($p)
    {
        // 启动事务
        Db::startTrans();
        try {
            //单据表
            $gzdj = [
                'djbm'     => $p['djbm'],
                'dj_date'       => $p['dj_date'],
                'user_id'            => $p['user_id'],
                'status'         => 1,
                'create_time'     => time(),
            ];
            //先判断是否已存在这个单据编号
            $res = Db::table('xs_gzdj')->where('djbm', $p['djbm'])->find();
            if ($res) {
                $gzdj['update_time'] = time();
                Db::table('xs_gzdj')->where('djbm', $p['djbm'])->data($gzdj)->update();
            } else {
                //$res = Db::table('xs_gzdj')->insert($gzdj);
                $dj_id = Db::table('xs_gzdj')->insertGetId($gzdj);
            }

            //再写入 客户工作表-当日
            if (isset($p['comp1'])) {
                $arr = $p['comp1'];
                foreach ($arr as $v) {
                    $v['djbm'] = $p['djbm'];
                    $v['rz_date'] = $p['dj_date'];
                    $v['user_id'] = $p['user_id'];
                    $v['type'] = 1;                 //当天工作
                    if (isset($v['jk_userid'])) {
                        if (empty($v['jk_userid']))  unset($v['jk_userid']);
                        if (is_array($v['jk_userid'])) $v['jk_userid'] = implode(',', $v['jk_userid']);
                    }
                    //先判断是否已经存在这个id号数据
                    if ($v['id'] > 0) {
                        $v['update_time'] = time();
                        Db::table('xs_khgz')->where('id', $v['id'])->data($v)->update();
                    } else {
                        unset($v['id']);
                        $khgz_id = Db::table('xs_khgz')->insertGetId($v);
                    }
                }
            }
            if (isset($p['comp4'])) {
                //客户工作表-次日
                $arr = $p['comp4'];
                foreach ($arr as $v) {
                    $v['djbm'] = $p['djbm'];
                    $v['rz_date'] = $p['dj_date'];
                    $v['user_id'] = $p['user_id'];
                    $v['type'] = 2;                 //次日工作
                    if (isset($v['jk_userid'])) {
                        if (empty($v['jk_userid']))  unset($v['jk_userid']);
                        if (is_array($v['jk_userid'])) $v['jk_userid'] = implode(',', $v['jk_userid']);
                    }
                    //先判断是否已经存在这个id号数据
                    if ($v['id'] > 0) {
                        $v['update_time'] = time();
                        Db::table('xs_khgz')->where('id', $v['id'])->data($v)->update();
                    } else {
                        unset($v['id']);
                        $khgz_id = Db::table('xs_khgz')->insertGetId($v);
                    }
                }
            }

            //再写入 其它工作表-当日
            if (isset($p['comp2'])) {
                $arr = $p['comp2'];
                foreach ($arr as $v) {
                    $v['djbm'] = $p['djbm'];
                    $v['rz_date'] = $p['dj_date'];
                    $v['user_id'] = $p['user_id'];
                    $v['type'] = 1;                 //当天工作
                    if (isset($v['jk_userid'])) {
                        if (empty($v['jk_userid']))  unset($v['jk_userid']);
                        if (is_array($v['jk_userid'])) $v['jk_userid'] = implode(',', $v['jk_userid']);
                    }
                    //先判断是否已经存在这个id号数据
                    if ($v['id'] > 0) {
                        $v['update_time'] = time();
                        Db::table('xs_qtgz')->where('id', $v['id'])->data($v)->update();
                    } else {
                        unset($v['id']);
                        $qtgz_id = Db::table('xs_qtgz')->insertGetId($v);
                    }
                }
            }
            if (isset($p['comp5'])) {
                $arr = $p['comp5'];
                foreach ($arr as $v) {
                    $v['djbm'] = $p['djbm'];
                    $v['rz_date'] = $p['dj_date'];
                    $v['user_id'] = $p['user_id'];
                    $v['type'] = 2;                 //次日工作
                    if (isset($v['jk_userid'])) {
                        if (empty($v['jk_userid']))  unset($v['jk_userid']);
                        if (is_array($v['jk_userid'])) $v['jk_userid'] = implode(',', $v['jk_userid']);
                    }
                    //先判断是否已经存在这个id号数据
                    if ($v['id'] > 0) {
                        $v['update_time'] = time();
                        Db::table('xs_qtgz')->where('id', $v['id'])->data($v)->update();
                    } else {
                        $qtgz_id = Db::table('xs_qtgz')->insertGetId($v);
                    }
                }
            }

            //再写入 工作问题表
            if (isset($p['comp3'])) {
                $arr = $p['comp3'];
                foreach ($arr as $v) {
                    $v['djbm'] = $p['djbm'];
                    $v['rz_date'] = $p['dj_date'];
                    $v['user_id'] = $p['user_id'];
                    if (isset($v['jk_userid'])) {
                        if (empty($v['jk_userid']))  unset($v['jk_userid']);
                        if (is_array($v['jk_userid'])) $v['jk_userid'] = implode(',', $v['jk_userid']);
                    }
                    //先判断是否已经存在这个id号数据
                    if ($v['id'] > 0) {
                        $v['update_time'] = time();
                        Db::table('xs_gzwt')->where('id', $v['id'])->data($v)->update();
                    } else {
                        $gzwt_id = Db::table('xs_gzwt')->insertGetId($v);
                    }
                }
            }

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \think\Exception("写数据出错，" . $e, 10006);   //手动抛出异常,$e->getMessage()
        }
        return 1;
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     */
    public function read($id) {}

    /**
     * 保存更新的资源
     *
     * @param  int $id
     */
    public function update($id) {}

    /**
     * 删除指定资源
     *
     * @param  int $id
     */
    public function delete($id)
    {
        try {
            $res = Db::table('xs_gzdj')->where('id', $id)
                ->useSoftDelete('delete_time', time())
                ->delete();
        } catch (\Exception $e) {
            throw new \think\Exception("XsgzService.delete 删除数据出错，" . $e->getMessage(), 10006);   //手动抛出异常
        }
        return $res;
    }

    /**
     * 保存更新 客户工作khgz 的资源
     *
     * @param  int $id
     */
    public function updateKhgz($data)
    {
        try {
            $data['update_time'] = time();
            $res = Db::table('xs_khgz')->where('id',  $data['id'])->data($data)->update();
        } catch (\Exception $e) {
            throw new \think\Exception("XsgzService.updateKhgz 更新数据出错，" . $e->getMessage(), 10006);   //手动抛出异常
        }
        return $res;
    }

    /**
     * 删除 客户工作khgz 表指定资源
     *
     * @param  int $id
     */
    public function deleteKhgz($id)
    {
        try {
            $res = Db::table('xs_khgz')->where('id',  $id)
                ->useSoftDelete('delete_time', time())
                ->delete();
        } catch (\Exception $e) {
            throw new \think\Exception("XsgzService.deleteKhgz 删除数据出错，" . $e->getMessage(), 10006);   //手动抛出异常
        }
        return $res;
    }

    /**
     * 保存更新 其它工作qtgz 的资源
     *
     * @param  int $id
     */
    public function updateQtgz($data)
    {
        try {
            $data['update_time'] = time();
            $res = Db::table('xs_qtgz')->where('id',  $data['id'])->data($data)->update();
        } catch (\Exception $e) {
            throw new \think\Exception("XsgzService.updateQtgz 更新数据出错，" . $e->getMessage(), 10006);   //手动抛出异常
        }
        return $res;
    }

    /**
     * 删除 其它工作qtgz 表指定资源
     *
     * @param  int $id
     */
    public function deleteQtgz($id)
    {
        try {
            $res = Db::table('xs_qtgz')->where('id',  $id)
                ->useSoftDelete('delete_time', time())
                ->delete();
        } catch (\Exception $e) {
            throw new \think\Exception("XsgzService.deleteQtgz 删除数据出错，" . $e->getMessage(), 10006);   //手动抛出异常
        }
        return $res;
    }

    /**
     * 保存更新 工作问题gzwt 的资源
     *
     * @param  int $id
     */
    public function updateGzwt($data)
    {
        try {
            $data['update_time'] = time();
            $res = Db::table('xs_gzwt')->where('id',  $data['id'])->data($data)->update();
        } catch (\Exception $e) {
            throw new \think\Exception("XsgzService.updateGzwt 更新数据出错，" . $e->getMessage(), 10006);   //手动抛出异常
        }
        return $res;
    }

    /**
     * 删除 工作问题gzwt 表指定资源
     *
     * @param  int $id
     */
    public function deleteGzwt($id)
    {
        try {
            $res = Db::table('xs_gzwt')->where('id',  $id)
                ->useSoftDelete('delete_time', time())
                ->delete();
        } catch (\Exception $e) {
            throw new \think\Exception("XsgzService.deleteKhgz 删除数据出错，" . $e->getMessage(), 10006);   //手动抛出异常
        }
        return $res;
    }
}
